package cn.ejiakanghu.agedmallrefundserver.rabbitmq;

import cn.ejiakanghu.agedmallrefundserver.wx.WxRefundService;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * @author houzb
 * @Description
 * @create 2021-11-25 16:40
 */
@Component
@RabbitListener(queues = "wx_refund_queue")
public class WxRefundMqReceiver {

    protected static Logger logger = LoggerFactory.getLogger(WxRefundMqReceiver.class);

    @Autowired
    private WxRefundService wxRefundService;

    @RabbitHandler
    public void process(String refundData) {
        logger.debug("wx_refund_queue 接收消息: " + refundData);
        String afterSaleId = JacksonUtil.parseString(refundData, "afterSaleId");
        if(StringUtils.isNotBlank(afterSaleId)){
            String bigOrderCode = JacksonUtil.parseString(refundData, "bigOrderCode");
            String price = JacksonUtil.parseString(refundData, "price");
            BigDecimal priceDecimal = new BigDecimal(price);
            wxRefundService.afterSaleRefund(Integer.parseInt(afterSaleId), priceDecimal, bigOrderCode);
        }
        else{
            String bigOrderCode = JacksonUtil.parseString(refundData, "bigOrderCode");
            String refundMoney = JacksonUtil.parseString(refundData, "refundMoney");
            String childOrderCode = JacksonUtil.parseString(refundData, "childOrderCode");
            BigDecimal refundFee = new BigDecimal(refundMoney);
            wxRefundService.refund(bigOrderCode, childOrderCode, refundFee);
        }
        try {
            Thread.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
